<template>
  <div class="camerist">
    <el-card>
      <el-form :disabled="camerist.isPass === 1" ref="camerist" :model="camerist" label-width="80px">
        <el-form-item label="电话">
          <el-input v-model="camerist.phone" type="text"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input
            @blur="checkEmail(camerist.email)"
            v-model="camerist.email"
            type="text"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="camerist.cameristName" type="text"></el-input>
        </el-form-item>
        <el-form-item label="证件照">
          <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :action="BASE_URL"
            class="avatar-uploader"
          >
            <img :src="camerist.avatar" style="width: 300px; height: 300px" />
          </el-upload>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number
            v-model="camerist.age"
            :min="1"
            :max="100"
            label="年龄"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="camerist.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="申请理由">
          <el-input type="textarea" v-model="camerist.introduce"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="isHaveApply" type="primary" @click="onSubmit">更新申请</el-button>
          <el-button v-if="!isHaveApply" type="primary" @click="onSubmit">立即申请</el-button>
          <el-button v-if="camerist.isPass !== 1" @click="clear">清空</el-button>
          <el-tag v-if="camerist.isPass === 2" style="float: right;margin-top: 5px;" type="info">申请中</el-tag>
          <el-tag v-if="camerist.isPass === 1" style="float: right;margin-top: 5px;" type="success">申请成功</el-tag>
          <el-popover
            style="float: right;margin-top: 5px;"
            v-if="camerist.isPass === 0"
            placement="top-start"
            width="200"
            trigger="hover"
            :content="camerist.reason">
            <el-button slot="reference">查看驳回理由</el-button>
          </el-popover>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import camerist from '@/api/camerist'
import store from '@/store'

export default {
  data() {
    return {
      camerist: {
        gender: '男',
        age: 0,
        phone: '',
        email: '',
        introduce: '',
        avatar: '',
        userId: this.$store.state.user.userId,
        isCamerist: 0,
      },
      reason: '',
      isHaveApply: false,
      BASE_URL: process.env.VUE_APP_BASE_API + "/weddingoss/fileoss/uploadFile",
    };
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      camerist.getApplyByUserId(this.$store.state.user.userId).then( res => {
        if ( res.data.have ) {
          this.isHaveApply = true
          this.camerist = res.data.camerist
          this.reason = this.camerist.reason
          if ( this.camerist.isPass === 1 ) {
            store.commit("setIsCamerist")
            store.commit("setCamerist", this.camerist)
          }
        } else {
          this.isHaveApply = false
        }
      })
    },
    handleAvatarSuccess(res) {
      this.camerist.avatar = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    onSubmit() {
        if ( this.camerist.introduce !== '' 
        && this.camerist.avatar !== '' 
        && this.camerist.cameristName !== '' 
        && this.checkModbile(this.camerist.phone)
        && this.checkEmail(this.camerist.email)) {
          camerist.applyCamerist(this.camerist).then( res => {
            if ( res.success ) {
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.init()
            }
            this.$router.push({path: '/camera'})
          })
        } else {
          this.$message({
            message: '请填写完整正确信息',
            type: 'info'
          })
        }
    },
    checkModbile(phone) {
      var re = /^1[2,3,4,5,6,7,8,9][0-9]{9}$/;
      var result = re.test(phone);
      return result;
    },
    checkEmail(email) {
      var re = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
      var result = re.test(email);
      return result;
    },
    clear() {
        this.camerist = {
        gender: '男',
        age: 0,
        phone: '',
        email: '',
        introduce: '',
        avatar: '',
        isPass: 0,
        reason: this.reason,
        userId: this.$store.state.user.userId,
      }
    }
  },
};
</script>

<style scoped>
.camerist {
  width: 60%;
  margin-left: 20%;
  margin-right: 20%;
  margin-top: 5%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>